package com.amazon.dao;

import com.amazon.bo.Orders;
import com.amazon.bo.Shop;
import com.amazon.dao.base.BaseDao;
import com.amazon.web.controller.vo.Order;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class OrdersDao extends BaseDao<Orders> {


    public int getOrdersCount(String conn) {
        Object results = execute(session -> {
            Query query = session.createNativeQuery("\n" +
                    "\n" +
                    "SELECT \n" +
                    "count(1)\n" +
                    "\n" +
                    "\n" +
                    "FROM orders so \n" +

                    " where "+conn+"  ");
            return query.getSingleResult();
        });
        return results != null ? Integer.parseInt(results.toString()) : 0;

    }
    public List<Orders> getAdminOrders(int start, int pagesize, String conn) {

        List<Orders> results = execute(session -> {

            Query query= session.createNativeQuery("SELECT *  "+
                    "FROM orders so \n"+
                    "where "+conn +" \n" +
                    "limit "+start+"," +pagesize+"\n",Orders.class);


            return  (List<Orders>)query.getResultList();
        });
        return results != null ? results : new ArrayList<>();
    }

    public Orders getOrderByAmazonId(String id) {
        List<Orders> results = execute(session -> session
                .createNativeQuery("select * from orders where amazon_order_id=:id", Orders.class)
                .setParameter("id",id)
                .getResultList());

        return results.size()>0?results.get(0):null;

    }

    //供应链产品列表
    public List<Orders> getProductList(String conn,final int page, final int pageSize) {
        List<Orders> results = execute(session -> (List<Orders>) session
                .createNativeQuery("SELECT *  FROM orders WHERE 1=1 "
                        +conn+" ORDER BY id  ", Orders.class)
                .setFirstResult((page - 1) * pageSize)
                .setMaxResults(pageSize)
                .getResultList());
        return results != null ? results : new ArrayList<Orders>();
    }

}
